package com.sunday.aomicaichang.ui;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.squareup.picasso.Picasso;
import com.sunday.common.base.BaseActivity;
import com.sunday.common.model.ResultDO;
import com.sunday.common.utils.MD5Utils;
import com.sunday.common.utils.ToastUtils;
import com.sunday.common.widgets.taosty.Toasty;
import com.sunday.aomicaichang.R;
import com.sunday.member.constv.ConstValue;
import com.sunday.member.entity.order.OrderItem;
import com.sunday.member.http.MemberClient;
import com.sunday.member.utils.SignUtil;

import butterknife.Bind;
import butterknife.ButterKnife;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by yun_sheng on 2017/6/5.
 */

public class MallRechangeDetailActivity extends BaseActivity {
    @Bind(R.id.img_top)
    ImageView imgTop;
    @Bind(R.id.tv_order_status)
    TextView tvOrderStatus;
    @Bind(R.id.tv_type)
    TextView tvType;
    @Bind(R.id.img)
    ImageView img;
    @Bind(R.id.tv_name)
    TextView tvName;
    @Bind(R.id.tv_integral)
    TextView tvIntegral;
    @Bind(R.id.tv_time)
    TextView tvTime;
    @Bind(R.id.tv)
    TextView tv;
    @Bind(R.id.tv_integral_num)
    TextView tvIntegralNum;
    @Bind(R.id.btn_next)
    TextView btnNext;
    @Bind(R.id.order_no)
    TextView orderNo;

    private long orderId;
    private OrderItem orderItem;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_exchange_detail);
        ButterKnife.bind(this);
        orderId = getIntent().getLongExtra("orderId", 0);
        getData();
    }

    private void getData() {
        String nonce = MD5Utils.random();
        String[] str1 = new String[]{"orderId", "nonce"};
        String[] str2 = new String[]{String.valueOf(orderId), nonce};
        showLoadingDialog(0);
        Call<ResultDO<OrderItem>> call = MemberClient.getApiAdapter().getOrderDetail(orderId, nonce, SignUtil.getSign(str1, str2));
        call.enqueue(new Callback<ResultDO<OrderItem>>() {
            @Override
            public void onResponse(Call<ResultDO<OrderItem>> call, Response<ResultDO<OrderItem>> response) {
                if (isFinish || response.body() == null) {
                    return;
                }
                dissMissDialog();
                if (response.body().getCode() == 0) {
                    if (response.body().getResult() == null) {
                        return;
                    }
                    orderItem = response.body().getResult();
                    updateView();

                } else {
                    ToastUtils.showToast(mContext, response.body().getMessage());
                }
            }

            @Override
            public void onFailure(Call<ResultDO<OrderItem>> call, Throwable t) {
                dissMissDialog();
            }
        });
    }
    private void updateView() {
        if (orderItem == null) {
            return;
        }
        tvOrderStatus.setText("订单状态:"+orderItem.getOrderStatusStr());
        tvType.setText(orderItem.getSendTypeStr()+" " + orderItem.getOrderNo());
        if(orderItem.getProducts()!=null) {
            if (!TextUtils.isEmpty(orderItem.getProducts().get(0).getImage())) {
                Picasso.with(mContext)
                        .load(orderItem.getProducts().get(0).getImage())
                        .error(R.mipmap.ic_product)
                        .into(img);
            }

            if(orderItem.getProducts().get(0).getName()!=null)
            tvName.setText(orderItem.getProducts().get(0).getName());
            tvIntegral.setText(orderItem.getProducts().get(0).getPrice()+"积分");
        }
        tvIntegralNum.setText(orderItem.getTotalAmount()+"积分");
        if(orderItem.getSendTypeStr()!=null)
        tv.setText(orderItem.getSendTypeStr());
        if(orderItem.getCreateTime()!=null)
        tvTime.setText("兑换时间:"+orderItem.getCreateTime());
        tvIntegralNum.setText(orderItem.getTotalAmount()+"积分");
        orderNo.setText("订单编号:"+orderItem.getOrderNo());
        if(orderItem.getOrderStatus()== ConstValue.STA_THREE){
            btnNext.setVisibility(View.VISIBLE);
        }else {
            btnNext.setVisibility(View.GONE);
        }
        btnNext.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String nonce=MD5Utils.random();
                String[] str1=new String[]{"orderId","nonce"};
                String[] str2=new String[]{String.valueOf(orderItem.getId()),nonce};
                showLoadingDialog(0);
                Call<ResultDO> call= MemberClient.getApiAdapter().confirmReceive(orderItem.getId(),nonce,SignUtil.getSign(str1,str2));
                call.enqueue(new Callback<ResultDO>() {
                    @Override
                    public void onResponse(Call<ResultDO> call, Response<ResultDO> response) {
                        if (isFinish||response.body()==null){return;}
                        dissMissDialog();
                        if (response.body().getCode()==0){
                            ToastUtils.showToast(mContext,"已确认收货");
                            Intent intent=new Intent(MallRechangeDetailActivity.this,MallRechangeSuccessActivity.class);
                            startActivity(intent);
                            finish();
                        }else {
                            Toasty.error(mContext,response.body().getMessage()).show();
                        }
                    }

                    @Override
                    public void onFailure(Call<ResultDO> call, Throwable t) {
                        dissMissDialog();
                        Toasty.error(mContext,"网络连接错误").show();
                    }
                });
            }
        });
         }
}
